package com.crane.app.config;


import com.crane.app.web.interceptor.AppSessionInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.config.annotation.*;

@Configuration
public class AppWebAppConfig implements WebMvcConfigurer {

    @Autowired
    private AppSessionInterceptor appSessionInterceptor;


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/statics/**").addResourceLocations("classpath:/statics/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                //.allowedOrigins(sysConfigProperties.getAllowOrigins().toArray(new String[sysConfigProperties.getAllowOrigins().size()]))
                .allowedMethods("GET", "POST", "OPTIONS")
                .allowCredentials(true)
                .exposedHeaders("X-AUTH-SESSION", "Content-Security-Policy", "X-Content-Type-Options", "X-XSS-Protection")
                .allowedHeaders("Origin", "X-Requested-With", "Content-Type", "Accept", "X-AUTH-SESSION", "Content-Security-Policy", "X-Content-Type-Options", "X-XSS-Protection")
                .maxAge(3600);
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.defaultContentType(MediaType.APPLICATION_JSON);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(appSessionInterceptor);
    }

}
